package com.sooncode.project.focusservice.controller;

import com.alibaba.fastjson.JSON;
import com.sooncode.project.core.finder.Page;
import com.sooncode.project.focusservice._.base.Utils;
import com.sooncode.project.focusservice.model.Test;
import com.sooncode.project.focusservice.model.TestData;
import com.sooncode.project.focusservice.service.DownloadReportService;
import com.sooncode.project.focusservice.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    private TestService service;
    @Autowired
    private DownloadReportService reportService;

    @PostMapping("test")
    public boolean add(@RequestParam Map map){
        map.put("data", JSON.parseArray(map.get("items").toString(), TestData.class));
        return service.add(map);
    }
    @PutMapping("test")
    public boolean modify(String id,@RequestParam Map map){
        return service.modify(id,map);
    }
    @DeleteMapping("test")
    public boolean delete(@RequestParam String[] ids){
        for(String id:ids){
            service.delete(id);
        }
        return true;
    }
    @GetMapping("tests")
    public Page<Test> getPageList(@RequestParam int pageSize, @RequestParam int pageIndex, @RequestParam Map map){
        map.remove("pageSize");
        map.remove("pageIndex");
        Utils.mapValueTrim(map);
        return service.getPageList(pageSize,pageIndex-1,map);
    }
    @GetMapping("publicApi/test")
    public Test getById(@RequestParam String id){
        return service.getByPid(id);
    }
    @GetMapping("publicApi/checkTest")
    public List<Test> getCheckTest(@RequestParam String id){
        return service.getCheckTestByPid(id);
    }

    @GetMapping("publicApi/getClassReport")
    public void getClassReport(@RequestParam String classId, HttpServletResponse response){
        reportService.downloadReport(classId,response);
    }
}
